package com.hbt.myplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbt.myplus.auth.jwt.JWTUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.hbt.myplus.service.IDoctorService;
import com.hbt.myplus.service.impl.DoctorServiceImpl;
import com.hbt.myplus.model.Doctor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hbt.myplus.utils.ResponseWeb;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;

import javax.print.Doc;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author astupidcoder
 * @since 2020-12-22
 * @version v1.0
 */
@RestController
@RequestMapping("doctor")


public class DoctorController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DoctorServiceImpl doctorService;

//    @PostMapping(value = "/doctor/login")
    public ResponseWeb doctorLogin(@RequestBody Doctor doctor){
        Doctor doc = doctorService.getById(doctor.getId());
        if (doc == null){
            return ResponseWeb.fail("登录失败，找不到该医生");
        }
        Map<String, String> map = new HashMap<>(); //用来存放payload信息
        map.put("id",doc.getId().toString());
        map.put("realname",doc.getRealname());
        map.put("avatar",doc.getAvatar());

        // 生成token令牌
        String token = JWTUtil.generateToken(map);

        return new ResponseWeb(token);
    }

    /**
     * 查询分页数据
     */
    @GetMapping(value = "/s")
    public ResponseWeb<Page> findListByPage(@RequestParam(name = "pageNum", defaultValue = "1") int pageNum,@RequestParam(name = "pageSize", defaultValue = "20") int pageSize){
        return null;
    }


    @PostMapping(value = "/change/avatar")
    public ResponseWeb changeAvatar(@RequestBody Map map){
        if (map == null){
            return ResponseWeb.fail("更新头像失败");
        }
        Doctor doctor =  doctorService.getById((String)map.get("docId"));
        if (doctor !=  null){
            doctor.setAvatar((String)map.get("url"));
            doctorService.updateById(doctor);
            return new ResponseWeb("头像更新成功");
        }else {
            return ResponseWeb.fail("更新头像失败");
        }


    }

    @GetMapping(value = "/all")
    public ResponseWeb<Doctor> listAll(@RequestParam(value = "type", required = false)String type){
        List<Doctor> doctorList = doctorService.list();
        if (type != null && type != ""){
            doctorList = doctorList.stream().filter(a -> a.getPosition().equals("医生")).collect(Collectors.toList());
        }
        return new ResponseWeb(doctorList);
    }

    /**
     * 根据id查询
     */
    @GetMapping(value = "/{id}")
    public ResponseWeb<Doctor> getById(@PathVariable("id") String id){
       return new ResponseWeb<>(doctorService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseWeb<Doctor> add(@RequestBody Doctor doctor){
        return null;
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseWeb<String> delete(@RequestParam("ids") List<String> ids){
        return null;
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseWeb<Doctor> update(@RequestBody Doctor doctor){
        return null;
     }

}
